<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<link rel="stylesheet" type="text/css" href="css/purchasing.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left backbtn"></a>
			<h1 class="mui-title">购买货物</h1>
		</header>
		<div class="mui-content">
			<div class="content">
				<div class="mui-input-row trade">
					<label class="trade_name"></label>
					<select class="trade_count mui-input" name="" id="num">
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7</option>
						<option value="8">8</option>
						<option value="9">9</option>
						<option value="10">10</option>
					</select>
					<div class="mui-icon mui-icon-arrowdown"></div>
				</div>
				<!--<div class="mui-input-row trade xygm">
					<label class="trade_name">请选择延后付款期限</label>
					<input id='account' type="text" class="daycount mui-input" placeholder="" value="123">
					<div class="daytext">天</div>
				</div>-->
				<div class="total">
					物品总价值为：<label></label>元
				</div>
				<!--<div class="tip xygm">
					延期支付贷款至：7天后 2018-08-08 12:00前
				</div>-->
				<form class="mui-input-group">
					<div class="mui-input-row mui-checkbox mui-left">
						<label>同意<a data-href="/app/agreement?key=purchase" id="agreement">《火速购商城电子购销合同》</a></label>
						<input class="readtext" name="checkbox" value="Item 1" type="checkbox">
					</div>
				</form>
			</div>
			<div class="btn xygm">
				<button type="button" class="mui-btn mui-btn-yellow mui-btn-block">信用购买</button>
			</div>
			<div class="btn gmhw">
				<button type="button" class="mui-btn mui-btn-block">微信支付</button>
			</div> 
		</div>
		<script src="../lib/mui.min.js"></script>
		<script type="text/javascript" src="../lib/jquery-1.11.0.js"></script>
		<script type="text/javascript" src="../lib/service.js"></script>
		<script type="text/javascript">
			mui.init()
			//			放在plusready里,否则会报错
			var data;
			var subData
			mui.plusReady(function() {
				plus.navigator.setStatusBarBackground("#FF7234");
				var str = plus.webview.currentWebview().parms;
				console.log(str)
				data = JSON.parse(str);
				if(JSON.parse(str).type == 0) {
					hidexygm();
				} else {
					showxygm()
				}
//				alert(data.num)
				$('#num').val(data.num)
				$('.total label').html(parseInt(data.data.goods.price) * data.num)
				subData = {
					goodId: data.data.goods.id,
					goodNo: data.data.goods.no,
					goodName: data.data.goods.name,
					goodNumber :data.num,
					money :parseInt(data.data.goods.price) * data.num
				}
				$('.trade_name').html(data.data.goods.name);
				$('.total label').html(parseInt(data.data.goods.price) * data.num)
				$('#num').bind('change', function() {
					console.log($(this).val())
					var total = parseInt(data.data.goods.price) * $(this).val()
					$('.total label').html(total)
					subData.goodNumber = $(this).val();
					subData.money = total;
				})
			});
			var flag = false
			mui('.mui-input-group').on('change', 'input', function() {
				var value = this.checked ? "true" : "false";
				console.log(value)
				flag = value
				//value 是否选中 ;
			});	
			$('#agreement').bind('tap',function(){
				openurl('../home/agreement.html',JSON.stringify({
					title:$(this).html(),
					url:$(this).attr('data-href')
				}))
			})
			//隐藏信用购买  并显示直接购买
			function hidexygm() {
				$(".xygm").hide();
				$(".gmhw").show();
			}

			//显示信用购买  并隐藏直接购买
			function showxygm() {
				$(".xygm").show();
				$(".gmhw").hide();
			}
			//直接购买
			$('.gmhw button').bind('tap', function() {
				subData.productMoney = data.data.goods.price;
				subData.payWay = 0;
				delete subData.money
				postServer('app/member/buyOrder', $.param(subData), function(res) {
					if(res.success) {
//						mui.toast("提交成功")
//						plus.webview.currentWebview().close()login.css
					}

				}, function(res) {
					console.log(JSON.stringify(res))
					mui.toast(res.message)
				})
				mui.toast("暂未开放微信支付")	
			})
			//信用购买
			$('.xygm button').bind('tap', function() {
				if(!flag) {
					mui.toast("请同意购销合同")
					return;
				}
				postServer('app/member/applyApplication', $.param(subData), function(res) {
					if(res.success) {
						mui.toast("提交成功")
						plus.webview.currentWebview().close()
					}

				}, function(res) {
					console.log(JSON.stringify(res))
					mui.toast(res.message)
				})
			})
		</script>
	</body>

</html>